import mqtt from "mqtt";
const client = mqtt.connect("wss://rhwebagent.cn:39003", {
  clientId: Math.random().toString().slice(8) - 0,
  username: 'poc',
  password: 'poc@2025',
  rejectUnauthorized: false
});



client.on("connect", () => {
  console.log("连接成功");
  client.subscribe("poc/agent/#");
  const jsonstr = {
    "content": "我冷了啊。",
    "id": Math.random().toString().slice(8) - 0,
    "requires_confirmation": false,
    "state": {
      "空调温度": "16",
      "氛围灯": "",
      "童锁": "",
      "香氛控制": "0",
      "音乐播放曲目": "",
      "音乐播放音量": "40",
      "座椅靠背角度": "43",
      "座椅位置": "30",
      "天窗控制": "18",
      "计时器": "0",
      "车窗调整": "8"
    },
    "tone": "贴心女友",
    "user_id": "web_js_test",
    "type": 2
  }
  setTimeout(() => {
    client.publish("poc/agent/chat/request/web_device1", JSON.stringify(jsonstr));
    
  }, 2000)
});

client.on("message", (topic, message) => {
  console.log(topic, JSON.parse(message.toString()));
});

client.on("error", (error) => {
  console.error("MQTT连接错误:", error);
});

client.on("reconnect", () => {
  console.log("正在尝试重新连接...");
});

client.on("offline", () => {
  console.log("MQTT客户端已离线");
});